// pages/main/storehouse/outxq.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        startDate: '',
        endDate: '',
        date: '',
        dateend: '',
        goodsno1: '',
        model: {
            xsname: "",
            goodsname: "",
            goodsno: ""
        },
        goods: [],
        url: getApp().globalData.apiurl,
        source: 0,
        loadingType: "more",
        totalCount: 0,
        totalPages: 0,
        pageSize: 10,
        pageIndex: 1,
        listData: [],
        scrollHeight: 0,
        pageNum: 0,
        total: 0
    },
    n2: function (e) {
        var t = new Date,
            o = t.getFullYear(),
            a = t.getMonth() + 1,
            r = t.getDate();
        return "start" === e ? o -= 10 : "end" === e && (o += 10), a = a > 9 ? a : "0" + a, r = r > 9 ? r : "0" + r, "".concat(o, '-').concat(a, '-').concat(r)
    },
    bindDateChange: function (e) {
        console.log("开始时间")
        if (this.handleTime(true, e.detail.value)) return this.setData({
            date: e.detail.value,
        })
        wx.showToast({
            title: "开始时间不得大于结束时间！",
            icon: "none",
            duration: 2000
        });
    },
    // 结束时间
    bindDateChangeend: function (e) {
        if (this.handleTime(false, e.detail.value)) return this.setData({
            dateend: e.detail.value,
        })

        wx.showToast({
            title: "结束时间不得小于开始时间！",
            icon: "none",
            duration: 2000
        });
    },
    // 时间判断
    handleTime(state, time) {
        let _that = this;
        let t = [];
        let t2 = [];
        let enums = ''; // 结束时间
        let snum = ''; // 开始时间
        if (state) {
            t2 = time.split("-"); // 当前选中的开始时间
            t = _that.data.dateend.split("-");
        } else {
            t = time.split("-");
            t2 = _that.data.date.split("-");
        }
        t.forEach((item, index) => {
            enums += item;
            snum += t2[index];
        })
        if (parseInt(enums) == parseInt(snum)) return true;
        if (parseInt(enums) > parseInt(snum)) return true;
        return false;
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var n = this;
        n.setData({
            startDate: this.n2("start"),
            endDate: this.n2("end"),
            date: this.n2({
                format: !0
            }),
            dateend: this.n2({
                format: !0
            }),
        });
        let times = {
            endPurchasetime: n.data.dateend + " 24:00:00",
            beginPurchasetime: n.data.date + " 00:00:00"
        }
        const pars = {
            pageNum: n.data.pageIndex,
            pageSize: n.data.pageSize,
            timerange: times,
            state: 2,
        }
        console.log(pars)
        wx.request({
            url: n.data.url + "goodsmaintain/goods/list",
            method: "GET",
            data: {
                type: 2
            },
            success: function (t) {
                n.setData({
                    goods: t.data.rows
                })

                console.log(n.goods)
            },
            fail: function () {
                console.log("请求失败")
            },
            complete: function () {
                console.log("请求完成")
            }
        });
        wx.request({
            url: n.data.url + "receiptdoc/receiptdoc/list",
            data: pars,
            method: "GET",
            success: function (t) {
                t.data.rows.forEach(item => {
                    item.createBy = n.data.goods.find(gtem => gtem.goodsname === item.goodsName).specifications
                    n.data.listData.push(item)
                });
                n.setData({
                    listData: t.data.rows,
                    pageIndex: n.data.pageIndex + 1,
                    pageNum: t.data.total / n.data.pageSize + 1,
                    total: t.data.total
                })
            },
            fail: function () {},
            complete: function () {}
        });
        wx.getSystemInfo({
            success: function (res) {
                n.setData({
                    scrollHeight: res.windowHeight - 30
                })
            }
        });
    },

    binggoods: function (e) {
        let sel = e.detail;
        this.setData({
            goodsno1: sel.__args__[0]
        })
        this.goodsno1 = sel.__args__[0]
        console.log(this.goodsno1)
    },

    submitForm: function () {
        console.log("sdasda")
        var e = this;
        // if (e.goodsno1 == '' || e.goodsno1 == undefined || e.goodsno1 == null) return;
        console.log(e.goodsno1)
        this.listData = [];
        // t.getStorageSync("key"), getApp().globalData.userinfo.roles[0].roleKey, getApp().globalData.userinfo.nickName;

        let times = {
            endPurchasetime: e.data.dateend + " 24:00:00",
            beginPurchasetime: e.data.date + " 00:00:00"
        }
        let pars = {
            goodsno: e.data.goodsno1,
            pageNum: 1,
            pageSize: 10,
            timerange: times,
            state: 2,
        }
        console.log(pars)
        wx.request({
            url: e.data.url + "receiptdoc/receiptdoc/list",
            method: "GET",
            data: pars,
            success: function (t) {
                t.data.rows.forEach(item => {
                    item.createBy = e.data.goods[e.data.goods.findIndex(gtem => gtem.goodsname === item.goodsName)].specifications
                    e.data.listData.push(item)
                });
                // e.data.pageNum+
                e.setData({
                    pageNum: 1,
                    pageSize: e.data.total / e.data.pageSize,
                    listData: t.data.rows,
                    total: t.data.total
                })
            },
            fail: function () {},
            complete: function () {}
        })
    },

    loadMoreData() {
        // 请求新数据
        let n = this;
        let times = {
            endPurchasetime: n.data.dateend + " 24:00:00",
            beginPurchasetime: n.data.date + " 00:00:00"
        }
        const pars = {
            goodsno: n.data.goodsno1,
            pageNum: n.data.pageIndex,
            pageSize: n.data.pageSize,
            timerange: times,
            state: 2,
        }
        // console.log(n.data.pageIndex < n.data.pageNum)
        console.log(n.data.pageIndex, n.data.pageNum)
        if (n.data.pageIndex < n.data.pageNum) {
            wx.request({
                url: n.data.url + "receiptdoc/receiptdoc/list",
                data: pars,
                method: "GET",
                success: function (t) {
                    // console.log("12312")
                    t.data.rows.forEach(item => {
                        item.createBy = n.data.goods.find(gtem => gtem.goodsname === item.goodsName).specifications
                        n.data.listData.push(item)
                    });
                    n.setData({
                        listData: n.data.listData,
                        pageIndex: n.data.pageIndex + 1,
                        total: n.data.total
                    })

                },
                fail: function () {},
                complete: function () {}
            })
        }
    },
})